<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Index action | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'actions-index.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/actions-index.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/actions-index.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/actions-index.html" rel="nofollow" target="_blank">../en/actions-index.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-link"><a href="actions.html">Actions</a></span>
»
<span class="breadcrumb-node">Index action</span>
</div>
<div class="navheader">
<span class="prev">
<a href="actions-webhook.html">« Webhook action</a>
</span>
<span class="next">
<a href="actions-logging.html">Logging Action »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="actions-index"></a>Index action<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/actions/index.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>Use the <code class="literal">index</code> action to index data into Elasticsearch.
See <a class="xref" href="actions-index.html#index-action-attributes" title="Index action attributes">Index action attributes</a> for the supported attributes.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_configuring_index_actions"></a>Configuring index actions<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/actions/index.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following snippet shows a simple <code class="literal">index</code> action definition:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"actions" : {
  "index_payload" : { <a id="CO525-1"></a><i class="conum" data-value="1"></i>
    "condition": { ... }, <a id="CO525-2"></a><i class="conum" data-value="2"></i>
    "transform": { ... }, <a id="CO525-3"></a><i class="conum" data-value="3"></i>
    "index" : {
      "index" : "my-index", <a id="CO525-4"></a><i class="conum" data-value="4"></i>
      "doc_id": "my-id" <a id="CO525-5"></a><i class="conum" data-value="5"></i>
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO525-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The id of the action</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO525-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>An optional <a class="xref" href="condition.html" title="Conditions">condition</a> to restrict action execution</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO525-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>An optional <a class="xref" href="transform.html" title="Payload transforms">transform</a> to transform the payload and prepare the data that should be indexed</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO525-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>The elasticsearch index to store the data to</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO525-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>An optional <code class="literal">_id</code> for the document, if it should always be the same document.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="index-action-attributes"></a>Index action attributes<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/actions/index.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Required</th>
<th align="left" valign="top">Default</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">index</code></p></td>
<td align="left" valign="top"><p>yes</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>The Elasticsearch index to index into.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">doc_id</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>The optional <code class="literal">_id</code> of the document.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">execution_time_field</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>The field that will store/index the watch execution
                                                      time.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">timeout</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>60s</p></td>
<td align="left" valign="top"><p>The timeout for waiting for the index api call to
                                                      return. If no response is returned within this time,
                                                      the index action times out and fails. This setting
                                                      overrides  the default  timeouts.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">refresh</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>Optional setting of the <a href="docs-refresh.html" class="ulink" target="_top">refresh policy</a>
                                                      for the write request</p></td>
</tr>
</tbody>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="anatomy-actions-index-multi-doc-support"></a>Multi-document support<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/actions/index.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Like with all other actions, you can use a <a class="xref" href="transform.html" title="Payload transforms">transform</a> to replace
the current execution context payload with another and by that change the document
that will end up indexed.</p>
<p>The index action plays well with transforms with its support for the special <code class="literal">_doc</code>
payload field.</p>
<p>When resolving the document to be indexed, the index action first looks up for a
<code class="literal">_doc</code> field in the payload. When not found, the payload is indexed as a single
document.</p>
<p>When a <code class="literal">_doc</code> field exists, if the field holds an object, it is extracted and indexed
as a single document. If the field holds an array of objects, each object is treated as
a document and the index action indexes all of them in a bulk.</p>
<p>An <code class="literal">_index</code>, or <code class="literal">_id</code> value can be added per document to dynamically set the index and ID
of the indexed document.</p>
<p>The following snippet shows a multi-document <code class="literal">index</code> action definition:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"actions": {
  "index_payload": {
    "transform": {
      "script": """
      def documents = ctx.payload.hits.hits.stream()
        .map(hit -&gt; [
          "_index": "my-index", <a id="CO526-1"></a><i class="conum" data-value="1"></i>
          "_id": hit._id, <a id="CO526-2"></a><i class="conum" data-value="2"></i>
          "severity": "Sev: " + hit._source.severity <a id="CO526-3"></a><i class="conum" data-value="3"></i>
        ])
        .collect(Collectors.toList());
      return [ "_doc" : documents]; <a id="CO526-4"></a><i class="conum" data-value="4"></i>
      """
    },
    "index": {} <a id="CO526-5"></a><i class="conum" data-value="5"></i>
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO526-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The document’s index</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO526-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>An optional <code class="literal">_id</code> for the document</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO526-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>A new <code class="literal">severity</code> field derived from the original document</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO526-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>The payload <code class="literal">_doc</code> field which is an array of documents</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO526-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>Since the <code class="literal">_index</code> was informed per document this should be empty</p>
</td>
</tr>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="actions-webhook.html">« Webhook action</a>
</span>
<span class="next">
<a href="actions-logging.html">Logging Action »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>